<?php
    error_reporting(E_ALL);
    header("Content-type: text/html; charset=utf-8");
    include 'MySqlHelper.php';
    
    $sql;
    $NavName=$_POST["NavName"];
    $PID=$_POST['PID'];
    $ImgUrl=$_POST['ImgUrl'];
    $OrderID=$_POST['OrderID'];
    $NavigateUrl=$_POST['NavigateUrl'];
    $Target=$_POST['Target'];
    $Remark=$_POST['Remark'];
    $Status=$_POST['Status'];
    $EditUrl=$_POST['EditUrl'];
    $ID=$_POST['ID'];

    $MySqlHelper = new MySqlHelper();
    switch ($_POST["action"])
    {
    	case "Mod":
    	 	$sql=<<<sqlStr
    		    update NavTree set 
    		    NavName = '$NavName',
    		    PID=$PID,
    		    ImgUrl='$ImgUrl',
    		    OrderID=$OrderID,
    		    NavigateUrl='$NavigateUrl',
    		    Target='$Target',
    		    Remark='$Remark',
    		    Status=$Status, 
    		    EditUrl='$EditUrl' 
    		    where ID = $ID
sqlStr;
            OperateResult($MySqlHelper->ExecSql($sql));
    		break;
    	case "Add":
    		$sql=<<<sqlStr
    		    insert into NavTree
    		    (
    		        NavName,
    		        PID,
    		        ImgUrl,
    		        OrderID,
    		        NavigateUrl,
    		        Target,
    		        Remark,
    		        Status,
    		        EditUrl
    		    )
    		     values
    		    (
    				'$NavName',
    				$PID,
    			    '$ImgUrl',
    			    $OrderID,
    			    '$NavigateUrl',
    			    '$Target',
    			    '$Remark',
    			    '$Status',
    			    '$EditUrl'
    			)
sqlStr;
    		OperateResult($MySqlHelper->ExecSql($sql));
       		break;
    	default:
    		if(!$MySqlHelper->Exists('select ID from NavTree where PID = '.$ID))
    		{
    		    $sql='delete from NavTree where ID = '.$ID;
    		    //echo $sql;
    		    OperateResult($MySqlHelper->ExecSql($sql));
    		}
    		else 
    		{
    			echo '<script type="text/javascript">alert("请先删除此节点下的数据！");history.go(-1);</script>';
    		}
    		break;
    }
    //echo '<p>'.$sql.'</p>';
    
    function OperateResult($result)
    {
    	if($result>0)
    	{
    		echo '<script type="text/javascript">alert("操作成功！");location.href="index.php";</script>';
    	}
    	else 
    	{
    		echo '<script type="text/javascript">alert("操作失败！");history.go(-1);</script>';
    	}
    }